package com.aia.prm.service;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import com.cfh.util.Tools;

public class MemoryLog {
	private static final Logger logger = Logger.getLogger(MemoryLog.class);

	private static LinkedList<String> logList = new LinkedList<String>();
	private static final int MAX_LOG = 500;
	{
		debug("程序启动");
	}

	public static void debug(String log) {
		String str = Tools.getDateTime() + ":" + log;
		logger.debug("[MemeryLog] " + log);
		logList.addFirst(str);
		ensureSize();
	}

	private static void ensureSize() {
		if (logList.size() > MAX_LOG) {
			logList.removeLast();
		}

		if (logList.size() > MAX_LOG) {
			logList.removeLast();
		}
	}

	public static List<String> getAllLogs() {
		return Collections.unmodifiableList(logList);
	}

	public static String fetchLogs() {
		StringBuilder sb = new StringBuilder();
		if (logList != null) {
			for (String item : logList) {
				sb.append(item).append("\r\n");
			}
		} else {
			sb.append("ERROR: logList is null");
		}
		return sb.toString();
	}
}
